package descentFly.away3D4.controller
{
	import descentFly.away3D4.action.df_action;
	import descentFly.away3D4.chips.df_chipAway;
	import descentFly.away3D4.df_awaySpace;
	import descentFly.away3D4.doors.df_door;
	import descentFly.chips.df_chip;
	import descentFly.controller.df_doorController;
	import descentFly.data.df_fields;
	import descentFly.df_space;
	
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	
	public class df_doorAway extends df_doorController {
		
		public static function create(a_stage	: Stage,
									  a_space	: df_space,
									  a_options: Object=null): df_doorAway {
			return new df_doorAway(a_stage, a_space, a_space, a_options);
		}
		
		public function df_doorAway(a_stage:Stage, a_space	: df_space, dispatchTarget:EventDispatcher=null, a_options: Object=null) {
			super(a_stage, a_space, dispatchTarget, a_options);
		}
		
		public function get awaySpace(): df_awaySpace {
			return dispatcher as df_awaySpace;
		}
		
		protected function get door(): df_door {
			return body as df_door;
		}
		
		override protected function checkNearest(): Boolean {
			if (body && door.body) {
				return awaySpace.forEach(function(object: df_chipAway, i: int): Boolean {
					if (!(object is df_door) && object.body) {
						var dest: Number = door.body.currentState.position.subtract(object.body.currentState.position).length;
						if (dest < door.options[df_door.FIELD_NEAREST]) return true;
					}
					return false;
				}, df_chipAway);
			}
			return false;
		}
		
		override protected function requestOpen():void {
			super.requestOpen();
//			door.setEnableBody(false);
			door.beginAction(df_door.ACTION_OPEN_DOOR);
			playSound(df_fields.FIELD_OPEN, door);
		}

		override protected function requestClose():void {
			super.requestClose();
//			door.setEnableBody(true);
			door.beginAction(df_door.ACTION_OPEN_DOOR, df_action.SP_INVERSE);
			playSound(df_fields.FIELD_CLOSE, door);
		}
	}
}